/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package dBasea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Medikua {

	public List<String> getKlinikak() throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izenaKlinika FROM Klinika order by izenaKlinika";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		return x;
	}

	public List<String> getMedikuak() throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Medikua order by izena";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		return x;
	}

	public boolean existitzenDaMedikua(String nanMediku) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT nan FROM Medikua WHERE nan='" + nanMediku + "'";
		ResultSet rs = dbk.executeSQL(query);
		boolean aurki = rs.next();
		return aurki;
	}

	public void medikuaSortu(String nan, String izena, String abizenak,
			Integer telefonoa) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "INSERT INTO Medikua (nan, izena, abizenak, telefonoa)"
				+ " VALUES ('" + nan + "','" + izena + "', '" + abizenak
				+ "' , " + telefonoa + " )";

		dbk.executeSQL(query);

	}

	public int getMedikua(String nanMediku) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT medikuZenb FROM Medikua WHERE nan='" + nanMediku
				+ "'";
		ResultSet rs = dbk.executeSQL(query);
		int md = 0;
		while (rs.next()) {
			md = rs.getInt("medikuZenb");
		}
		return md;
	}

	public void medikuaEsleitu(String id, String hira, Long has, Long buk)
			throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();

		String query = "INSERT INTO MedikuKlinika (medikuZenb, idKlinika, hasData, bukData)"
				+ " VALUES ( (SELECT medikuZenb FROM Medikua WHERE nan='"
				+ id
				+ "'),(SELECT idKlinika FROM Klinika WHERE izenaKlinika='"
				+ hira + "'), " + has + " , " + buk + " )";

		dbk.executeSQL(query);

	}

}
